// pages/MyHomePage/MyHomePage.js
const util = require("../../utils/util.js");
var app = getApp();
const requestManager = require("../../utils/request.js");
var router = require('../../utils/router.js');
const pageName = "myHomePage"

Page({
  /**
    * 页面的初始数据
    */
  data: {
    // 用于iPhone X适配
    isIpx: app.globalData.isIpx ? true : false,
    openType: "getUserInfo", // button open-type
    phoneNum: "", // 用户当前绑定的手机号
    noticeTitle: "" // 通知标题
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    // 头像及微信名获取的回调
    app.userInfoReadyCallback = res => {
      this.setData({
        wxUserInfo: res.userInfo,
      })
    };

    //获取头像
    if (app.globalData.userInfo) {
      this.setData({
        wxUserInfo: app.globalData.userInfo,
      })
    };

    // 显示绑定的手机号(获取用户微信信息通常为异步的,因此这里不会出现频繁setData的问题)
    this.data.phoneNum = util.getUserInfo().mobile;
    if (this.data.phoneNum != "") {
      this.setData({
        phoneNum: this.data.phoneNum,
      })
    };
    app.loginResultReadyCallback = res => {
      // 用户登录成功的回调
      this.setData({
        phoneNum: res.mobile,
      })
    };
  },

  onShow: function () {
    // 获取通知内容
    this._getNoticeContent();

    if (app.globalData.isShowBack) {
      app.globalData.isShowBack = false;
    };

    if (this.data.phoneNumChanged == true) {
      this.data.phoneNumChanged = false;
      // 用户重新绑定了手机
      this.setData({
        phoneNum: this.data.phoneNum,
      })
    };

  },

  /**
   * 获取通知详情
   */
  _getNoticeContent: function () {
    let params = {
      channel: "wxapp"
    };
    requestManager.getDataResult(requestManager.notice_latest, params, "GET")
      .then(data => {
        let noticeId = data.noticeId;
        if (noticeId != undefined && noticeId != null && noticeId != 0) {
          this.data.noticeId = noticeId;
          this.setData({
            noticeTitle: data.noticeTitle
          });
        } else {
          this.setData({
            noticeTitle: ""
          });
        }
      }).catch(e => {

      });
  },

  //跳转绑卡
  jumpBindCard: function (sender) {
    let self = this;
    //读取登录信息
    // if (!app.globalData.isAuthorize) {
    //   this.setData({
    //     openType: "getUserInfo"
    //   });
    //   return;
    // } else if (this.data.openType != "") {
    //   this.setData({
    //     openType: ""
    //   });
    // }

    //执行页面跳转
    if (sender.currentTarget.id == 1) {
      //订单
      let url = "/pages/orderList/orderList";
      router.route.setAction(router.NAVIGATE_TO)
        .setUrl(url)
        .run();
    } else if (sender.currentTarget.id == 2) {
      //地址pages/AddressList/AddressList
      let url = "/pages/AddressList/AddressList";
      router.route.setAction(router.NAVIGATE_TO)
        .setUrl(url)
        .run();

    } else if (sender.currentTarget.id == 3) {
      //我记录
      // let url = "/pages/CardsCenter/CardsCenter";
      // router.route.setAction(router.NAVIGATE_TO)
      //   .setReportName("CardsCenter")
      //   .setUrl(url)
      //   .run();

      // util.reportAnalytics("to_cards_center");
      // console.log("55555555")

    } else if (sender.currentTarget.id == 4) {
      //收款设置
      let url = "/pages/SettingPayAccount/SettingPayAccount";
      router.route.setAction(router.NAVIGATE_TO)
        .setReportName("SettingPayAccount")
        .setUrl(url)
        .run();
      util.reportAnalytics("to_Setting_account");
      console.log("44444")
    }

  },

  /**
   * 绑定手机号
   */
  bindPhoneNum: function (e) {
    if (!app.globalData.isAuthorize) {
      this.setData({
        openType: "getUserInfo"
      });
      return;
    } else if (this.data.openType != "") {
      this.setData({
        openType: ""
      });
    }
    let url = "/pages/PhoneVerificationPage/PhoneVerificationPage";
    router.route.setAction(router.NAVIGATE_TO)
      .setReportName("PhoneVerificationPage")
      .setUrl(url)
      .run();
  },

  /**
   * 联系客服
   */
  callServiceOnline: function () {
    util.reportAnalytics("call_service_online_home");
  },

  /**
   * 表单的相关处理
   */
  formSubmit: util.formSubmit,

  /**
   * 为确保用户授权获取头像信息的回调函数
   */
  getUserInfo: function (e) {
    if (app.globalData.isAuthorize || !e.detail.userInfo) {
      return;
    };

    util.userLogin().then(function () { }, reject => {
      setTimeout(function () {
        util.userLogin();
      }, 1000);
    });

    app.globalData.userInfo = e.detail.userInfo;
    app.globalData.isAuthorize = true;
    // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
    this.setData({
      wxUserInfo: e.detail.userInfo,
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    return {
      path: '/pages/PersonalCenter/PersonalCenter',
      imageUrl: 'https://bn.t.ivision.vip/static/image/wls/tag.png', 
      // imageUrl: 'https://vls.t.ivision.vip/static/image/wls/tag.png', 
      title: "微礼说，礼尚往来新方式。让心意更动人，送礼更方便。"
    }
  },

  /**
   * 用户重新绑定手机号后
   * 修改当前页绑定的手机号的数据
   */
  ex_updatePhoneData: function (phoneNum) {
    this.data.phoneNumChanged = true;
    this.data.phoneNum = phoneNum;
    let loginResult = util.getUserInfo();
    loginResult.mobile = phoneNum;
    util.setUserInfo(loginResult);
  },
  /**
    * 返回按钮点击
    */
  pageBack: function (e) {
      wx.navigateBack()

  }
})